package com.cheyun.netsalev3.repository

import com.cheyun.netsalev3.bean.ChannelApplyList
import com.cheyun.netsalev3.bean.ChannelListParam
import com.cheyun.netsalev3.bean.basedata.BaseDataParam
import com.cheyun.netsalev3.bean.dms.DmsUser
import com.cheyun.netsalev3.bean.login.LoginParam
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction1

class SwitchingDealersRepository: BaseRepository() {
    //获取通道列表
    fun getData(kFunction1: KFunction1<@ParameterName(name = "item") List<ChannelListParam>, Unit>) {
        networkState.postValue(NetworkState.LOADING)
        ApiClient.getInstance().getChannelmember()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<List<ChannelListParam>>>() {
                    override fun onSuccess(t: BaseResp<List<ChannelListParam>>) {
                        networkState.postValue(NetworkState.LOADED)
                        t.data?.apply {
                            kFunction1(this)
                        }

                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }


                })
    }

    /**
     * 获取正在申请的经销商
     */
    fun getApply(mid:Int, KFunction0: KFunction1<@ParameterName(name = "param") List<ChannelApplyList>, Unit>,fail:()->Unit) {
        ApiClient.getInstance().getJoinList(mid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<List<ChannelApplyList>>>() {
                    override fun onSuccess(t: BaseResp<List<ChannelApplyList>>) {
                        t.data?.apply(KFunction0)
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }
                })
    }

    fun cancelJoin(id: String, moth: () -> Unit) {
        ApiClient.getInstance().cancelJoin(id).subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    //切换经销商
    fun channelhang(channel: Int, moth: () -> Unit) {
        ApiClient.getInstance().getChannelchange(channel)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<LoginParam>>() {
                    override fun onSuccess(t: BaseResp<LoginParam>) {
                        t.data?.cauthkey?.apply {
                            MySharedPreferences.putCauthkey(this)
                            MySharedPreferences.putLoginData(t.data!!)
                        }
                        t.data?.member?.workbench?.apply {
                            MySharedPreferences.setWorkbench(this)
                        }
                        basData()
                        moth()
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }



    /**
     * 获取基础数据
     */
    fun basData() {
        ApiClient.getInstance().baseData(ApiStores.ATTRIBUTE)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<BaseDataParam>>() {
                    override fun onSuccess(t: BaseResp<BaseDataParam>) {
                        t.data?.apply {
                            MySharedPreferences.putBaseData(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })

    }
}